
#include "Series.h"

Series::Series(const BString& name) : BObject(name)
{
	_dirty = true;
	_visible = true;
	_lineWidth = 1;
	_lineColor.set(0, 0, 0);
}
Series::~Series()
{

}

void Series::setVisible(bool visible)
{
	if (_visible != visible)
	{
		_visible = visible;
		if (_visible)
			this->emit(Signal_Show, true);
		else
			this->emit(Signal_Hide, false);
	}
}
bool Series::visible() const
{
	return _visible;
}

void Series::setUnit(const BString& unit)
{
	_unit = unit;
}
const BString& Series::unit() const
{
	return _unit;
}

void Series::setLineWidth(float lineWidth)
{
	if (_lineWidth != lineWidth)
	{
		_lineWidth = lineWidth;
		_dirty = true;
	}
}
float Series::lineWidth() const
{
	return _lineWidth;
}

void Series::setLineColor(const BColor& color)
{
	if (_lineColor != color)
	{
		_lineColor = color;
		_dirty = true;
		this->emit(Signal_ColorChanged, color);
	}
}
const BColor& Series::lineColor() const
{
	return _lineColor;
}

void Series::insertValue(BReal key, BReal value)
{
	_valueMap.insert(key, value);
	this->emit(Signal_Changed);
}
void Series::replaceValue(int pos, BReal value)
{
	auto it = _valueMap.seek(pos);
	it.value() = value;
	this->emit(Signal_Changed);
}
void Series::removeValue(int pos, int count)
{
	_valueMap.remove(pos, count);
	this->emit(Signal_Changed);
}

void Series::setValueMap(const RealMap& valueMap)
{
	_valueMap = valueMap;
	this->emit(Signal_Changed);
}
const RealMap& Series::valueMap() const
{
	return _valueMap;
}

void Series::clearValue()
{
	_valueMap.clear();
	this->emit(Signal_Changed);
	this->emit(Signal_Clear);
}

BReal Series::interpValue(BReal key)
{
	auto prev = _valueMap <= key;
	auto next = _valueMap >= key;
	BReal deltX = next.key() - prev.key();
	BReal deltY = next.value() - prev.value();
	BReal value = (deltY * (key - prev.key())) / deltX   + prev.value();
	return value;
}

